package cn.doitedu.controller;

import cn.doitedu.pojo.LogStatistic;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class LogCollectionQualityController {

    Connection connection;

    public LogCollectionQualityController() throws SQLException {

        connection = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit46", "root", "root");
    }


    @RequestMapping("/api/log_statistic_report")
    @ResponseBody
    public String reportLogStatistic(@RequestBody LogStatistic logStatistic) throws SQLException {

        PreparedStatement pst = connection.prepareStatement("insert into log_collection_quality values (?,?,?,?)");

        pst.setString(1,logStatistic.getStatisticDate());
        pst.setString(2,logStatistic.getLogServerHostname());
        pst.setInt(3,logStatistic.getLogFileCount());
        pst.setInt(4,logStatistic.getLogLineAmount());

        pst.execute();
        pst.close();

        return "SUCCESS";

    }


    @RequestMapping("/api/get_collection_dq")
    @ResponseBody
    public List<LogStatistic> getCollectionDqData() throws SQLException {
        ArrayList<LogStatistic> lst = new ArrayList<>();

        PreparedStatement pst = connection.prepareStatement("select * from log_collection_quality");
        ResultSet resultSet = pst.executeQuery();
        while(resultSet.next()){

            String statisticDate = resultSet.getString("statistic_date");
            String hostname = resultSet.getString("statistic_logserver_hostmane");
            int fileCount = resultSet.getInt("statistic_file_count");
            int lineAmount = resultSet.getInt("statistic_line_amount");

            lst.add(new LogStatistic(hostname,statisticDate,fileCount,lineAmount));


        }

        return lst;

    }



    @RequestMapping("/api/log_hdfs_statistic_report")
    @ResponseBody
    public String reportHDFSStatistic(@RequestBody LogStatistic logStatistic) throws SQLException {
        try {
            // {"statisticDate":"2024-03-22" ,"logFileCount":2 ,"logLineAmount":8}
            PreparedStatement pst = connection.prepareStatement("insert into log_hdfs_collection_quality values (?,?,?)");
            pst.setString(1, logStatistic.getStatisticDate());
            pst.setInt(2, logStatistic.getLogFileCount());
            pst.setInt(3, logStatistic.getLogLineAmount());

            pst.execute();

            return "SUCCESS";
        }catch (Exception e){
            return "ERROR";
        }
    }




    @RequestMapping("/api/log_hdfs_distinct_statistic_report")
    @ResponseBody
    public String reportHDFSDistinctStatistic(@RequestBody LogStatistic logStatistic) throws SQLException {
        try {
            // {"statisticDate":"2024-03-22" ,"logFileCount":2 ,"logLineAmount":8}
            PreparedStatement pst = connection.prepareStatement("insert into log_hdfs_distinct_quality values (?,?,?)");
            pst.setString(1, logStatistic.getStatisticDate());
            pst.setInt(2, logStatistic.getLogFileCount());
            pst.setInt(3, logStatistic.getLogLineAmount());

            pst.execute();

            return "SUCCESS";
        }catch (Exception e){
            return "ERROR";
        }
    }



    @RequestMapping("/api/single_table_dq_check")
    @ResponseBody
    public String singleTableDQCheckReport(@RequestBody String singleTableDqCheck) throws SQLException {

        JSONObject jsonObject = JSON.parseObject(singleTableDqCheck);

        String targetTable = jsonObject.getString("target_table");
        String targetDate = jsonObject.getString("target_date");
        String checkTime = jsonObject.getString("check_time");
        JSONObject checkResultObject = jsonObject.getJSONObject("check_result");


        PreparedStatement stmt = connection.prepareStatement("insert into single_table_dq_check values(?,?,?,?)");
        stmt.setString(1,targetTable);
        stmt.setString(2,targetDate);
        stmt.setString(3,checkTime);
        stmt.setString(4,checkResultObject.toJSONString());


        stmt.execute();


        return "SUCCESS";
    }


}
